package com.supply.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.supply.pojo.ResultBean;
import com.supply.pojo.SalesBill;
import com.supply.req.SalesBillReq;
import com.supply.service.SalesBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/salesBill")
public class SalesBillController {
    @Autowired
    private SalesBillService salesBillService;

    @GetMapping("/list")
    public ResultBean list(SalesBillReq salesBillReq){
        System.out.println("查找销售列表的参数：" + salesBillReq);
        PageHelper.startPage(salesBillReq.getPageNum(),salesBillReq.getPageSize());
        List<SalesBill> salesBills = salesBillService.queryByCondition(salesBillReq);
        return ResultBean.success(new PageInfo(salesBills));
    }

    @PostMapping("/saveOrUpdate")
    public ResultBean saveOrUpdate(@RequestBody SalesBill salesBill){
        System.out.println("新增或修改的数据：" + salesBill);
        salesBill.setCreateTime(new Date());
        int rs = salesBillService.saveOrUpdate(salesBill);
        return rs >= 1 ? ResultBean.success() : ResultBean.error("服务器异常");
    }

    @DeleteMapping("/delete/{id}")
    public ResultBean delete(@PathVariable("id") Long id){
        System.out.println("要删除销售表的id" + id);
        int rs = salesBillService.deleteById(id);
        return rs >= 1 ? ResultBean.success() : ResultBean.error("服务器异常");
    }

    @GetMapping("/queryAll")
    public ResultBean queryAll(){
        return ResultBean.success(salesBillService.queryAll());
    }

    //查询还没被出库审批的销售单
    @GetMapping("/queryNotProcessed")
    public ResultBean queryNotProcessed(){
        return ResultBean.success(salesBillService.queryNotProcessed());
    }

    @GetMapping("/queryById/{id}")
    public ResultBean queryAll(@PathVariable("id") Long id){
        return ResultBean.success(salesBillService.queryById(id));
    }

}
